﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Innova.Microfinanzas.BusinessEntities;

namespace MyCommerce.Business.Stores
{
    public class SupplierManager
    {
        private MyCommerceEntities vContext;

        public SupplierManager()
        {
            if (vContext == null)
            {
                vContext = new MyCommerceEntities();
                vContext.ContextOptions.LazyLoadingEnabled = false;
            }
        }

        public SupplierManager(bool lazyLoad)
        {
            if (vContext == null)
            {
                vContext = new MyCommerceEntities();
                vContext.ContextOptions.LazyLoadingEnabled = lazyLoad;
            }
        }

        public List<Supplier> GetSuppliers()
        {
            List<Supplier> colSuppliers = new List<Supplier>();

            var result = from st in vContext.Supplier
                         .Include("Country")
                         select st;
            if (result != null)
                colSuppliers = new List<Supplier>(result.AsEnumerable<Supplier>());

            return colSuppliers;
        }
        
        public Supplier GetSupplierByID(long IdSupplier)
        {
            Supplier objSupplier = new Supplier();
            var result = (from st in vContext.Supplier
                         where st.IdSupplier == IdSupplier
                         select st).First();
            objSupplier = (Supplier)result;
            return objSupplier;
        }

        public void SaveSupplier(Supplier objSupplier)
        {
            var validateResult = (from cl in vContext.Supplier
                                  where (cl.Name.CompareTo(objSupplier.Name) == 0)
                                  select cl).Count();
            if (validateResult > 0)
            {
                throw new Exception("Ya existe un proveedor con este nombre.");
            }

            vContext.AddToSupplier(objSupplier);
            vContext.SaveChanges();
        }

        public void UpdateSupplier(Supplier objSupplier)
        {
            var validateResult = (from cl in vContext.Supplier
                                  where (cl.Name.CompareTo(objSupplier.Name) == 0 && cl.IdSupplier != objSupplier.IdSupplier)
                                  select cl).Count();
            if (validateResult > 0)
            {
                throw new Exception("Ya existe un proveedor con este nombre.");
            }

            var vResult = (from st in vContext.Supplier
                           where st.IdSupplier == objSupplier.IdSupplier
                           select st).First();


            vResult.Name = objSupplier.Name;
            vResult.Address = objSupplier.Address;
            vResult.Phone = objSupplier.Phone;
            vResult.Email = objSupplier.Email;
            vResult.PersonContactName = objSupplier.PersonContactName;
            vResult.IdCountry = objSupplier.IdCountry;

            vContext.SaveChanges();
        }

        public void DeleteSupplier(long IdSupplier)
        {
            var vResult = (from st in vContext.Supplier
                           where st.IdSupplier == IdSupplier
                           select st).First();
            vContext.DeleteObject(vResult);
            vContext.SaveChanges();
        }

    }
}
